/*
* 四川生学教育科技有限公司
* Copyright (c) 2015-2025 Founder Ltd. All Rights Reserved.
*
* This software is the confidential and proprietary information of
* Founder. You shall not disclose such Confidential Information
* and shall use it only in accordance with the terms of the agreements
* you entered into with Founder.
*
*/
package cn.demo.kafka.demo2;

import org.apache.kafka.clients.producer.Partitioner;
import org.apache.kafka.common.Cluster;

import java.util.Map;

/**
 * @author WUANG (wa@sxw.cn)
 * @description PartitionUtil
 * @date 2018/11/8 16:26
 * @slogon 站在巨人的肩膀上
 * @since 2.0.0
 */
public class PartitionUtil implements Partitioner {

    // 分区数
    private static final Integer PARTITION_NUM = 6;

    @Override
    public int partition(String topic, Object key, byte[] keyBytes, Object value, byte[] valueBytes, Cluster cluster) {
        if (null == key) {
            return 0;

        }

        String keyValue = String.valueOf(key);
// key取模
        int partitionId = (int) (Long.valueOf(key.toString()) % PARTITION_NUM);
        return partitionId;

    }

    @Override
    public void close() {


    }

    @Override
    public void configure(Map<String, ?> configs) {


    }

}